package cn.kgc.controller.logic.administrative;

import cn.kgc.pojo.*;
import cn.kgc.service.administrative.wxw_carService;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: wang
 * @date: 2022/2/17
 * @description:
 */
@Controller
@RequestMapping("/car")
public class wxw_carController {

    @Resource
    private wxw_carService wxw_carService;

    @RequestMapping("/selCarGuanli")
    @ResponseBody
    public String selCar(){
        List<Carsinfo> carsinfos = wxw_carService.selCar();
        return JSON.toJSONString(carsinfos);
    }

    @RequestMapping("/yongche")
    @ResponseBody
    public boolean yongche(HttpSession session,
                          @RequestParam Integer ci_carid,
                          @RequestParam String usetime,
                          @RequestParam String reason,
                          @RequestParam String approver
                          ) throws ParseException {
        User user = (User)session.getAttribute("user");
        Jobtitle jobtitle = (Jobtitle)session.getAttribute("zhiWei");
        Carsapplication car = new Carsapplication(-1,ci_carid,user.getId(),jobtitle.getId(),Date.valueOf(usetime),reason,user.getName(),approver,0);
        boolean b = wxw_carService.addYongche(car);
        return b;
    }

    @RequestMapping("/showCar")
    @ResponseBody
    public String showCar(){
        List<Carsinfo> carsinfos = wxw_carService.selCar();
        return JSON.toJSONString(carsinfos);
    }
    @RequestMapping("/shanchu")
    @ResponseBody
    public boolean shanchu(@RequestParam Integer id){

        boolean b = wxw_carService.deteCar(id);
        return b;
    }
    @RequestMapping("/saveCar")
    @ResponseBody
    public boolean saveCar(@ModelAttribute Carsinfo carsinfo){
        boolean b = wxw_carService.addCar(carsinfo);
        return b;
    }
    @RequestMapping("/selCarID")
    @ResponseBody
    public String selCarID(Integer id){
        Carsinfo carsinfo = wxw_carService.selbyId(id);
        return JSON.toJSONString(carsinfo);
    }

    @RequestMapping("/updaCar")
    @ResponseBody
    public boolean updaCar(@ModelAttribute Carsinfo carsinfo){
        boolean b = wxw_carService.updaCar(carsinfo);
        return b;
    }

    @RequestMapping("/panduan")
    @ResponseBody
    public boolean panduan(@RequestParam String number){
        boolean panduan = wxw_carService.panduan(number);
        return panduan;
    }

    @RequestMapping("/yongchexq")
    @ResponseBody
    public String yongche(HttpSession session,Integer currentPage,Integer pageSize){
        PageHelper.startPage(currentPage,pageSize);
        User user = (User)session.getAttribute("user");
        List<Carsapplication> carsapplications = wxw_carService.selCha(user.getId());
        Jobtitle jobtitle = (Jobtitle)session.getAttribute("zhiWei");
        PageInfo<Carsapplication> pageInfo = new PageInfo<>(carsapplications);
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("user",user);
        map.put("ch",pageInfo);
        map.put("jobtitle",jobtitle);
        return JSON.toJSONStringWithDateFormat(map,"yyyy-MM-dd");
    }

    @RequestMapping("/updCa")
    @ResponseBody
    public boolean updCa(Integer id){
        System.out.println("修改"+id);
        boolean b = wxw_carService.updCa(id);
        return b;
    }

    /*导出excel*/
    @RequestMapping(value = "/Carexcel")
    public void export(HttpServletResponse response, HttpSession session) throws IOException {
        User user = (User)session.getAttribute("user");
        List<yongcarxq> yongcarxqs = wxw_carService.selCarsa(user.getId());
        System.out.printf("------------" + yongcarxqs.toString());
        HSSFWorkbook wb = new HSSFWorkbook();

        HSSFSheet sheet = wb.createSheet(user.getName()+"用车记录表");

        HSSFRow row = null;
        row = sheet.createRow(0);//创建第一个单元格
        row.setHeight((short) (26.25 * 20));
        row.createCell(0).setCellValue(user.getName()+"用车记录信息");//为第一行单元格设值

        /*为标题设计空间
         * firstRow从第1行开始
         * lastRow从第0行结束
         *
         *从第1个单元格开始
         * 从第3个单元格结束
         */
        CellRangeAddress rowRegion = new CellRangeAddress(0, 0, 0, 8);
        sheet.addMergedRegion(rowRegion);

      /*CellRangeAddress columnRegion = new CellRangeAddress(1,4,0,0);
      sheet.addMergedRegion(columnRegion);*/

        row = sheet.createRow(1);
        row.setHeight((short) (22.50 * 20));//设置行高
        row.createCell(0).setCellValue("Id");//为第一个单元格设值
        row.createCell(1).setCellValue("职位");//为第二个单元格设值
        row.createCell(2).setCellValue("车辆编号");//为第三个单元格设值
        row.createCell(3).setCellValue("使用时间");//为第四个单元格设值
        row.createCell(4).setCellValue("使用事由");//为第四个单元格设值
        row.createCell(5).setCellValue("申请人");//为第四个单元格设值
        row.createCell(6).setCellValue("审批人");//为第四个单元格设值
        row.createCell(7).setCellValue("申请状态");//为第四个单元格设值


        //设置日期格式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

        //遍历所获取的数据
        for (int i = 0; i < yongcarxqs.size(); i++) {
            row = sheet.createRow(i + 2);
            yongcarxq yongcarxq = yongcarxqs.get(i);
            row.createCell(0).setCellValue(yongcarxq.getId());
            row.createCell(1).setCellValue(yongcarxq.getJob());
            row.createCell(2).setCellValue(yongcarxq.getCar());
            row.createCell(3).setCellValue(dateFormat.format(yongcarxq.getUseTime()));
            row.createCell(4).setCellValue(yongcarxq.getApplicantName());
            row.createCell(5).setCellValue(yongcarxq.getReason());
            row.createCell(6).setCellValue(yongcarxq.getApprover());
            String zhutai="";
            if (yongcarxq.getState()==0){
                zhutai="审核中";
            }else if(yongcarxq.getState()==1){
                zhutai="通过";
            }else if(yongcarxq.getState()==-1){
                zhutai="拒绝";
            }else if(yongcarxq.getState()==-2){
                zhutai="自己取消";
            }
            row.createCell(7).setCellValue(zhutai);
        }
        sheet.setDefaultRowHeight((short) (16.5 * 20));

        //列宽自适应
        for (int i = 0; i <= 40; i++) {
            sheet.autoSizeColumn(i);
        }
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        OutputStream os = response.getOutputStream();
        response.setHeader("Content-disposition", "attachment;filename=请假记录表");//默认Excel名称
        wb.write(os);
        os.flush();
        os.close();
    }
}
